﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;

namespace Business.系统设置
{
    public partial class waijianpeizhiadd : Form
    {
        public MainForm m_mainform;
        public waijianpeizhiadd()
        {
            InitializeComponent();
        }
        DataTable dtwaijianzp;

        private float X;

        private float Y;

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                if (con.Controls.Count > 0)
                    setTag(con);
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {

                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
                if (currentSize <= 0)
                {
                    currentSize = 0.1f;
                }
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }

        }

        void Form1_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / X;
            float newy = this.Height / Y;
            setControls(newx, newy, this);
            this.Text = this.Width.ToString() + " " + this.Height.ToString();

        }
        private void waijianpeizhiadd_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(Form1_Resize); this.Resize += new EventHandler(Form1_Resize);

            X = this.Width;
            Y = this.Height;

            setTag(this);
            Form1_Resize(new object(), new EventArgs());//x,y可在实例化时赋值,最后这句是新加的，在MDI时有用


            dtwaijianzp = new DataTable();
            Query objquery = new Query();
            dtwaijianzp = objquery.GetTable("select * from waijianzp");
            inicbx(lvcsqy, "table_cllx");
            inicbx(listView2, "ZPZL");
            inicbxlvcsqy();
        }
        public void inicbxlvcsqy()
        {
            foreach (ListViewItem li in lvcsqy.Items)
            {
                for (int i = 0; i < dtwaijianzp.Rows.Count; i++)
                {
                    if (li.SubItems[0].Text == dtwaijianzp.Rows[i]["cllx"].ToString().Trim())
                    {
                        li.ForeColor = Color.Red;
                        break;
                    }
                }
            }
        }

        void inicbx(ListView cbx, string dataname)
        {
            cbx.Items.Clear();
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select * from {0}", dataname);
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //cbx.Items.Add(dt.Rows[i]["name"].ToString().Trim());
                //if (i == 0)
                //    cbx.Text = dt.Rows[0]["name"].ToString().Trim();

                ListViewItem li = new ListViewItem();
                //li.SubItems[0].Text = string.Format("{0}", i + 1);
                //li.SubItems.Add(dt.Rows[i]["table_1"].ToString().Trim());
                li.Text = string.Format("{0}", dt.Rows[i]["table_1"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["table_2"].ToString().Trim());
                cbx.Items.Add(li);
            }
        }

        private void but_delete_Click(object sender, EventArgs e)
        {
            if (lvcsqy.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择车辆类型");
                return;
            }
            string scllx="";
            scllx=lvcsqy.SelectedItems[0].SubItems[0].Text;
            foreach (ListViewItem li1 in listView2.SelectedItems)
            {
                bool bxz = false;
                foreach (ListViewItem li2 in listView1.Items)
                {
                    if (li1.Text == li2.Text)
                    {
                        bxz = true;
                        break;
                    }
                }
                if (!bxz)
                {
                    ListViewItem li = new ListViewItem();
                    li.Text = li1.Text;
                    li.SubItems.Add(li1.SubItems[1].Text);
                    listView1.Items.Add(li);
                }
            }
            string szpzlbh = "";
            string szpzlname = "";

            if (listView1.Items.Count > 0)
            {
                lvcsqy.SelectedItems[0].ForeColor = Color.Red;
            }
            else
            {
                lvcsqy.SelectedItems[0].ForeColor = Color.Black;
            }
            foreach (ListViewItem li in listView1.Items)
            {
                szpzlbh += li.Text + ",";
                szpzlname += li.SubItems[1].Text + ",";
            }
            szpzlbh = szpzlbh.TrimEnd(',');
            szpzlname = szpzlname.TrimEnd(',');
            bool bbz = false;
            for (int i = 0; i < dtwaijianzp.Rows.Count; i++)
            {
                if (scllx == dtwaijianzp.Rows[i]["cllx"].ToString().Trim())
                {
                    bbz = true;
                    //dtwaijianzp.Rows[i]["cllx"] = scllx;
                    dtwaijianzp.Rows[i]["zpbh"] = szpzlbh;
                    dtwaijianzp.Rows[i]["zpname"] = szpzlname;
                    break;
                }
            }
            if (!bbz)
            {
                DataRow dr = dtwaijianzp.NewRow();
                dr["cllx"] = scllx;
                dr["zpbh"] = szpzlbh;
                dr["zpname"] = szpzlname;
                dtwaijianzp.Rows.Add(dr);
            }
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (lvcsqy.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择车辆类型");
                return;
            }
            string scllx = "";
            scllx = lvcsqy.SelectedItems[0].SubItems[0].Text;
            foreach (ListViewItem li1 in listView1.SelectedItems)
            {
                listView1.Items.Remove(li1);
            }
                
            
            string szpzlbh = "";
            string szpzlname = "";
            if (listView1.Items.Count > 0)
            {
                lvcsqy.SelectedItems[0].ForeColor = Color.Red;
            }
            else
            {
                lvcsqy.SelectedItems[0].ForeColor = Color.Black;
            }
            foreach (ListViewItem li in listView1.Items)
            {
                szpzlbh += li.Text + ",";
                szpzlname += li.SubItems[1].Text + ",";
            }
            szpzlbh = szpzlbh.TrimEnd(',');
            szpzlname = szpzlname.TrimEnd(',');
            bool bbz = false;
            for (int i = 0; i < dtwaijianzp.Rows.Count; i++)
            {
                if (scllx == dtwaijianzp.Rows[i]["cllx"].ToString().Trim())
                {
                    if (szpzlbh.Length < 1)
                    {
                        dtwaijianzp.Rows[i].Delete();
                        return;
                    }
                    bbz = true;
                    //dtwaijianzp.Rows[i]["cllx"] = scllx;
                    dtwaijianzp.Rows[i]["zpbh"] = szpzlbh;
                    dtwaijianzp.Rows[i]["zpname"] = szpzlname;
                    break;
                }
            }
            if (!bbz)
            {
                DataRow dr = dtwaijianzp.NewRow();
                dr["cllx"] = scllx;
                dr["zpbh"] = szpzlbh;
                dr["zpname"] = szpzlname;
                dtwaijianzp.Rows.Add(dr);
            }
        }

        private void lvcsqy_SelectedIndexChanged(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            foreach (ListViewItem li in lvcsqy.SelectedItems)
            {
                for (int i = 0; i < dtwaijianzp.Rows.Count; i++)
                {
                    if (dtwaijianzp.Rows[i]["cllx"].ToString().Trim() == li.SubItems[0].Text)
                    {
                        string[] szpbh = dtwaijianzp.Rows[i]["zpbh"].ToString().Trim().Split(',');
                        string[] szpname = dtwaijianzp.Rows[i]["zpname"].ToString().Trim().Split(',');
                        for (int j = 0; j < szpbh.Length; j++)
                        {
                            if (szpbh[j].Trim().Length > 0)
                            {
                                ListViewItem li2 = new ListViewItem();
                                li2.Text = szpbh[j].Trim();
                                li2.SubItems.Add(szpname[j].Trim());
                                listView1.Items.Add(li2);
                            }
                        }
                    }
                }
            }
        }

        private void but_add_Click(object sender, EventArgs e)
        {
            
            Query objquery = new Query();
            DataTable dt = objquery.GetTable(string.Format("select * from waijianzp"));
            string sql = "";// string.Format("select * from waijianzp where cllx='{0}'");
            for (int i = 0; i < dtwaijianzp.Rows.Count; i++)
            {
                bool bbz = false; 

                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (dtwaijianzp.Rows[i]["cllx"].ToString().Trim() == dt.Rows[j]["cllx"].ToString().Trim())
                    {
                        bbz = true;
                        break;
                    }
                }
                if (bbz)
                {
                    sql = string.Format("update waijianzp set zpbh='{1}',zpname='{2}' where cllx='{0}'",
                        dtwaijianzp.Rows[i]["cllx"].ToString().Trim(),
                        dtwaijianzp.Rows[i]["zpbh"].ToString().Trim(),
                        dtwaijianzp.Rows[i]["zpname"].ToString().Trim());
                }
                else
                {
                    sql = string.Format("insert into waijianzp (cllx,zpbh,zpname) values ('{0}','{1}','{2}')",
                        dtwaijianzp.Rows[i]["cllx"].ToString().Trim(),
                        dtwaijianzp.Rows[i]["zpbh"].ToString().Trim(),
                        dtwaijianzp.Rows[i]["zpname"].ToString().Trim());

                }
                objquery.InsertDB(sql);
            }
            MessageBox.Show("保存成功");
        }
    }
}
